home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 1999 #5 / 1999 CD 5 (black).iso / Delphi3 / install / data.z / BATMOVE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-08-05  |  10.3 KB  |  386 lines

  1. unit batmove;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ExtCtrls, DBTables, DB, Buttons, Mask, Spin, ComCtrls;
  8.  
  9. type
  10.   TfrmBatchMove = class(TForm)
  11.     BatchMove1: TBatchMove;
  12.     Bevel1: TBevel;
  13.     Label1: TLabel;
  14.     Bevel2: TBevel;
  15.     Label2: TLabel;
  16.     Label3: TLabel;
  17.     Label4: TLabel;
  18.     Label5: TLabel;
  19.     Label6: TLabel;
  20.     cbSourceAlias: TComboBox;
  21.     cbSourceTable: TComboBox;
  22.     cbDestAlias: TComboBox;
  23.     cbDestTable: TComboBox;
  24.     dbSource: TDatabase;
  25.     tblSource: TTable;
  26.     dbDest: TDatabase;
  27.     tblDest: TTable;
  28.     Label7: TLabel;
  29.     cbMode: TComboBox;
  30.     Bevel3: TBevel;
  31.     Memo1: TMemo;
  32.     BitBtn1: TBitBtn;
  33.     BitBtn2: TBitBtn;
  34.     Label8: TLabel;
  35.     PageControl1: TPageControl;
  36.     TabSheet1: TTabSheet;
  37.     TabSheet2: TTabSheet;
  38.     ckAbortOnKeyViol: TCheckBox;
  39.     txtKeyViolTableName: TLabel;
  40.     edKeyViolTableName: TEdit;
  41.     ckAbortOnProblem: TCheckBox;
  42.     txtProblemTableName: TLabel;
  43.     edProblemTableName: TEdit;
  44.     edRecordCount: TSpinEdit;
  45.     Label11: TLabel;
  46.     Label12: TLabel;
  47.     edChangedTableName: TEdit;
  48.     Label13: TLabel;
  49.     Bevel4: TBevel;
  50.     OpenDialog1: TOpenDialog;
  51.     sbKeyViolTbl: TSpeedButton;
  52.     sbProbTbl: TSpeedButton;
  53.     edCommitCount: TSpinEdit;
  54.     sbChangeTbl: TSpeedButton;
  55.     ckTransliterate: TCheckBox;
  56.     Label9: TLabel;
  57.     cbSourceIndex: TComboBox;
  58.     Label10: TLabel;
  59.     cbDestIndex: TComboBox;
  60.     procedure FormCreate(Sender: TObject);
  61.     procedure cbModeChange(Sender: TObject);
  62.     procedure cbSourceAliasChange(Sender: TObject);
  63.     procedure BitBtn1Click(Sender: TObject);
  64.     procedure ckAbortOnKeyViolClick(Sender: TObject);
  65.     procedure ckAbortOnProblemClick(Sender: TObject);
  66.     procedure sbKeyViolTblClick(Sender: TObject);
  67.     procedure cbSourceTableChange(Sender: TObject);
  68.   private
  69.     { Private declarations }
  70.   public
  71.     { Public declarations }
  72.   end;
  73.  
  74. var
  75.   frmBatchMove: TfrmBatchMove;
  76.  
  77. const
  78.     sNONE = 5;        // '<NONE>'
  79.   sSOURCE = 6;            // 'Source'
  80.     sDESTINATION = 7;        // 'Destination'
  81.   sTBLNAMEREQ = 8;        // 'table name is required.'
  82.   sKEYVIOLTBLREQ = 9;    // 'Key Violation'
  83.   sPROBTBLREQ = 10;        // 'Problem'
  84.   sALIASREQ = 11;        // 'alias is required.'
  85.  
  86. implementation
  87.  
  88. {$R *.DFM}
  89.  
  90. procedure TfrmBatchMove.FormCreate(Sender: TObject);
  91. begin
  92.     Application.HintPause := 0;
  93.   
  94.     // Retreive aliases and store them in the Source/Destination comboboxes
  95.      with Session do
  96.      begin
  97.          if (not Active) then Active := TRUE;
  98.        GetAliasNames(cbSourceAlias.Items);
  99.        GetAliasNames(cbDestAlias.Items);
  100.      end;
  101.  
  102.     // Begin and set alias combobox entries to <NONE>
  103.   with cbSourceAlias do
  104.      begin
  105.          Items.Insert(0, LoadStr(sNONE));
  106.       ItemIndex := 0;
  107.      end;
  108.  
  109.      with cbDestAlias do
  110.      begin
  111.          Items.Insert(0, LoadStr(sNONE));
  112.       ItemIndex := 0;
  113.      end;
  114.  
  115.  
  116.      cbMode.ItemIndex := 0; // BatchMove mode is set to batAppend
  117.      cbModeChange(cbMode);  // Fill memo with information on BatchMove mode
  118.  
  119.     // Set options to TBatchMove's default property values
  120.     with BatchMove1 do
  121.      begin
  122.         ckAbortOnKeyViol.Checked := AbortOnKeyViol;
  123.          ckAbortOnProblem.Checked := AbortOnProblem;
  124.      ckTransliterate.Checked := Transliterate;
  125.   end;
  126.  
  127.     // Set possible maximum values dynamically
  128.   edRecordCount.MaxValue := High(LongInt);    // Maximum records to move
  129.   edCommitCount.MaxValue := High(Integer);  // Records moved before commit
  130.  
  131. end;
  132.  
  133. procedure TfrmBatchMove.cbModeChange(Sender: TObject);
  134. begin
  135.     // Display help information about the TBatchMode
  136.     with Memo1 do
  137.      begin
  138.          Clear;
  139.      Lines.Add(LoadStr(TComboBox(Sender).ItemIndex));
  140.        SendMessage(Handle, EM_LINESCROLL, 0, -1 * Lines.Count);
  141.      end;
  142. end;
  143.  
  144. procedure TfrmBatchMove.cbSourceAliasChange(Sender: TObject);
  145. var
  146.     cbTableList: TComboBox;
  147.      db: TDatabase;
  148. begin
  149.     if (TComboBox(Sender).ItemIndex = 0) then Exit;
  150.   if (Sender = cbSourceAlias) then
  151.       begin
  152.          cbTableList := cbSourceTable;
  153.           db := dbSource;
  154.         end
  155.   else if (Sender = cbDestAlias) then
  156.       begin
  157.          cbTableList := cbDestTable;
  158.           db := dbDest;
  159.         end
  160.   else Exit;
  161.  
  162.     cbTableList.Clear;
  163.   with db do
  164.   begin
  165.       if (Connected) then Connected := FALSE;
  166.         AliasName := TComboBox(Sender).Items.Strings[TComboBox(Sender).ItemIndex];
  167.         Connected := TRUE;
  168.   end;
  169.  
  170.     with cbTableList do
  171.   begin
  172.         Session.GetTableNames(db.DatabaseName, '', TRUE, FALSE, Items);
  173.         ItemIndex := 0;
  174.   end;
  175.  
  176.   cbSourceTableChange(cbTableList);
  177. end;
  178.  
  179. procedure TfrmBatchMove.BitBtn1Click(Sender: TObject);
  180. var
  181.     strName: String[80];
  182. begin
  183.     strName := SysUtils.EmptyStr;
  184.  
  185.     // Source and Destination alias must be specified.
  186.   if (cbSourceAlias.ItemIndex = 0) then
  187.       begin
  188.           strName := LoadStr(sSOURCE);
  189.         cbSourceAlias.SetFocus;
  190.      end
  191.   else if (cbDestAlias.ItemIndex = 0) then
  192.       begin
  193.           strName := LoadStr(sDESTINATION);
  194.         cbDestAlias.SetFocus;
  195.      end;
  196.   if (Length(strName) <> 0) then
  197.       begin
  198.          strName := Format('%s %s', [strName, LoadStr(sALIASREQ)]);
  199.         MessageDlg(strName, mtError, [mbOk], 0);
  200.         Exit;
  201.      end;
  202.   with dbSource do if (not Connected) then Connected := TRUE;
  203.   with dbDest do if (not Connected) then Connected := TRUE;
  204.  
  205.  
  206.     // Source and Destination table names must be specified.
  207.   if (Length(cbSourceTable.Text) = 0) then strName := LoadStr(sSOURCE)
  208.   else if (Length(cbDestTable.Text) = 0) then strName := LoadStr(sDESTINATION);
  209.  
  210.   if (Length(strName) <> 0) then
  211.       begin
  212.          MessageDlg(Format('%s %s', [strName, LoadStr(sTBLNAMEREQ)]), mtError,
  213.             [mbOk], 0);
  214.         Exit;
  215.      end;
  216.  
  217.   with tblSource, cbSourceIndex do
  218.   begin
  219.       TableName := cbSourceTable.Text;
  220.      if (ItemIndex <> 0) then IndexName := Text;
  221.   end;
  222.  
  223.   with tblDest, cbDestIndex do
  224.   begin
  225.       TableName := cbDestTable.Text;
  226.      if (ItemIndex <> 0) then IndexName := Text;
  227.   end;
  228.  
  229.  
  230.   // if AbortOnKeyViol or AbortOnProblem turned off, table must be specified
  231.   if ((not ckAbortOnKeyViol.Checked)
  232.       and (Length(edKeyViolTableName.Text) = 0)) then
  233.       begin
  234.         edKeyViolTableName.SetFocus;
  235.          MessageDlg(Format('%s %s', [LoadStr(sKEYVIOLTBLREQ),
  236.             LoadStr(sTBLNAMEREQ)]), mtError, [mbOk], 0);
  237.         Exit;
  238.      end
  239.   else
  240.       BatchMove1.KeyViolTableName := edKeyViolTableName.Text;
  241.  
  242.   if ((not ckAbortOnProblem.Checked)
  243.       and (Length(edProblemTableName.Text) = 0)) then
  244.       begin
  245.         edProblemTableName.SetFocus;
  246.          MessageDlg(Format('%s %s', [LoadStr(sPROBTBLREQ),
  247.             LoadStr(sTBLNAMEREQ)]), mtError, [mbOk], 0);
  248.         Exit;
  249.      end
  250.   else
  251.       BatchMove1.ProblemTableName := edProblemTableName.Text;
  252.  
  253.   // Set BatchMove properties as specified by user
  254.   with BatchMove1 do
  255.   begin
  256.         // Mode
  257.       case cbMode.ItemIndex of
  258.          0: Mode := batAppend;
  259.         1: Mode := batUpdate;
  260.         2: Mode := batAppendUpdate;
  261.         3: Mode := batCopy;
  262.         4: Mode := batDelete;
  263.      end;
  264.       AbortOnKeyViol := ckAbortOnKeyViol.Checked;        // AbortOnKeyViol
  265.      AbortOnProblem := ckAbortOnProblem.Checked;        // AbortOnProblem
  266.      Transliterate := ckTransliterate.Checked;            // Transliterate
  267.      RecordCount := edRecordCount.Value;                 // RecordCount
  268.      CommitCount := edCommitCount.Value;                    // CommitCount
  269.      ChangedTableName := edChangedTableName.Text;        // ChangedTableName
  270.   end;
  271.  
  272.   try
  273.       with dbDest do if (IsSQLBased) then StartTransaction;
  274.      BatchMove1.Execute;                                        // Perform BatchMove
  275.      with dbDest do if (InTransaction) then Commit;
  276.   except
  277.       on EDatabaseError do
  278.      begin
  279.          with dbDest do if (InTransaction) then Rollback;
  280.         raise;
  281.      end;
  282.   end;
  283. end;
  284.  
  285. procedure TfrmBatchMove.ckAbortOnKeyViolClick(Sender: TObject);
  286. var
  287.     bEnable: Boolean;
  288. begin
  289.     bEnable := (not TCheckBox(Sender).Checked);
  290.   edKeyViolTableName.Enabled := bEnable;
  291.   sbKeyViolTbl.Enabled := bEnable;
  292.  
  293.     if (bEnable) then
  294.       begin
  295.           txtKeyViolTableName.Font.Style := [];
  296.         edKeyViolTableName.SetFocus;
  297.      end
  298.   else
  299.       txtKeyViolTableName.Font.Style := [fsItalic]
  300. end;
  301.  
  302. procedure TfrmBatchMove.ckAbortOnProblemClick(Sender: TObject);
  303. var
  304.     bEnable: Boolean;
  305. begin
  306.     bEnable := (not TCheckBox(Sender).Checked);
  307.     edProblemTableName.Enabled := bEnable;
  308.   sbProbTbl.Enabled := bEnable;
  309.  
  310.     if (bEnable) then
  311.       begin
  312.           txtProblemTableName.Font.Style := [];
  313.         edProblemTableName.SetFocus;
  314.      end
  315.   else
  316.       txtProblemTableName.Font.Style := [fsItalic];
  317. end;
  318.  
  319. // This method is shared by sbKeyViolTbl, sbProbTbl, and sbChangeTbl
  320. // components. It allows client to designate a filename.
  321. procedure TfrmBatchMove.sbKeyViolTblClick(Sender: TObject);
  322. begin
  323.     with OpenDialog1 do
  324.   begin
  325.         if (not Execute) then Exit;
  326.      if (Sender = sbKeyViolTbl) then
  327.          edKeyViolTableName.Text := FileName
  328.      else if (Sender = sbProbTbl) then
  329.          edProblemTableName.Text := FileName
  330.      else if (Sender = sbChangeTbl) then
  331.          edChangedTableName.Text := FileName;
  332.   end;
  333. end;
  334.  
  335. (*    This method is shared by cbSourceTable and cbDestTable components.
  336.         When client designates a table, associated indexes are retreived into
  337.      appropriate comboboxes (e.g., cbSourceIndex or cbDestIndex. *)
  338. procedure TfrmBatchMove.cbSourceTableChange(Sender: TObject);
  339. var
  340.     cbIndexList: TComboBox;
  341.   Table: TTable;
  342.   db: TDatabase;
  343.     List: TStringList;
  344. begin
  345.     if (Length(TComboBox(Sender).Text) = 0) then Exit;
  346.  
  347.     // Update index list based on who is the caller.
  348.   if (Sender = cbSourceTable) then
  349.      begin
  350.         db := dbSource;
  351.         Table := tblSource;
  352.         cbIndexList := cbSourceIndex;
  353.      end
  354.   else if (Sender = cbDestTable) then
  355.      begin
  356.         db := dbDest;
  357.  
  358.         Table := tblDest;
  359.         cbIndexList := cbDestIndex;
  360.      end;
  361.   cbIndexList.Clear;
  362.  
  363.   // Because destination table may not exist, we'll first check for its
  364.   // existence before calling GetIndexNames to avoid exception.
  365.   Table.TableName := (TComboBox(Sender).Text);
  366.   List := nil;
  367.   try
  368.      List := TStringList.Create;
  369.      Session.GetTableNames(db.DatabaseName, '*.*', TRUE, FALSE, List);
  370.      if (List.IndexOf(Table.TableName) <> -1) then
  371.         Table.GetIndexNames(cbIndexList.Items);
  372.   finally
  373.      List.Free;
  374.   end;
  375.  
  376.   // The first index entry is set to <NONE>
  377.   with cbIndexList do
  378.   begin
  379.      Items.Insert(0, LoadStr(sNONE));
  380.      ItemIndex := 0;
  381.   end;
  382. end;
  383.  
  384. end.
  385.  
  386.